package com.pillpoppers.pillbox;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainFragmentActivity extends FragmentActivity  {
	
	private String currentFragmentTag;
	
	private addListener aL;
	
	interface addListener {
		public void addContact();
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragmentactivity_main);
		
		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		actionBar.addTab(actionBar.newTab()
				.setText("Schedule").setTabListener(new TabListener(new ScheduleFragment())).setTag("schedule"));
		actionBar.addTab(actionBar.newTab()
				.setText("Pills").setTabListener(new TabListener(new PillListFragment())).setTag("pills"));
		actionBar.addTab(actionBar.newTab()
				.setText("Contacts").setTabListener(new TabListener(new ContactsFragment())).setTag("contacts"));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_fragmentactivity, menu);
		
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public void onAttachFragment(Fragment fragment) {
		super.onAttachFragment(fragment);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_add: // add
			if (currentFragmentTag.equals("schedule")) {
				Intent intent = new Intent(this, AlarmSettingActivity.class);
				//intent.putExtra("scheduleId", 2);
				//intent.putExtra("pillId", 1);
				startActivity(intent);
			} else if (currentFragmentTag.equals("pills"))
				startActivity(new Intent(this, AddPillActivity.class));// change for testing
			else if (currentFragmentTag.equals("contacts")) {
				aL.addContact();				
			}

			return true;
		case R.id.action_setting:
			startActivity(new Intent(this, SettingsActivity.class));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public class TabListener implements ActionBar.TabListener {

		private Fragment fragment;
		
		public TabListener(Fragment fragment) {
			this.fragment = fragment;
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.replace(R.id.fragment_container, fragment);
			currentFragmentTag = tab.getTag().toString();
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(fragment);
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			
		}
		
	}

	
	public void setaddListener(addListener aL){
		this.aL = aL;
	}

}
